package com.zhny.protocol_v3_008.model;

import com.zhny.protocol_v3_008.constant.DTypeCode;
import com.zhny.protocol_v3_008.constant.PTypeCode;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * @author Alex
 * @date 2017年11月7日 下午9:35:37
 */
public class Datagram {
	public static final int HEAD_SIZE = 4;	
	public static final int PACKAGE_TYPE_SIZE = 2;
	public static final int DATE_SIZE = 4;
	public static final int DEVICE_FLAG_SIZE = 4;
	public static final int DEVICE_TYPE_SIZE = 2;
	public static final int DATA_LENGTH_SIZE = 2;
	public static final int CRC_SIZE = 4;
	public static final int END_SIZE = 4;
	private char[] head = {0x3F,0x40,0x72,0x37};
	private Short packageType;
	private Integer date;
	private Integer deviceFlag;
	private Short deviceType;
	private Short dataLength;
	private String data;
	private Long crc;
	private char[] end = {0x33,0x5E,0x54,0x24};
	public Datagram() {
		date = (int) (System.currentTimeMillis() / 1000);

	}
	public Datagram(short packageType, int deviceFlag, short deviceType) {
		super();
		this.packageType = packageType;
		this.deviceFlag = deviceFlag;
		this.deviceType = deviceType;
		date = (int) (System.currentTimeMillis() / 1000);

	}
	public char[] getHead() {
		return head;
	}
	public void setHead(char[] head) {
		this.head = head;
	}
	public Short getPackageType() {
		return packageType;
	}
	public void setPackageType(Short packageType) {
		this.packageType = packageType;
	}
	public Integer getDate() {
		return date;
	}
	public void setDate(Integer date) {
		this.date = date;
	}
	public Integer getDeviceFlag() {
		return deviceFlag;
	}
	public void setDeviceFlag(Integer deviceFlag) {
		this.deviceFlag = deviceFlag;
	}
	public Short getDeviceType() {
		return deviceType;
	}
	public void setDeviceType(Short deviceType) {
		this.deviceType = deviceType;
	}
	public Short getDataLength() {
		return dataLength;
	}
	public void setDataLength(Short dataLength) {
		this.dataLength = dataLength;
	}
	public String getData() {
		return data;
	}
	public void setData(String data) {
		this.data = data;
		setDataLength((short)data.length());
	}
	public Long getCrc() {
		return crc;
	}
	public void setCrc(long code) {
		this.crc = code;
	}
	public char[] getEnd() {
		return end;
	}
	public void setEnd(char[] end) {
		this.end = end;
	}
	@Override
	public String toString() {
		return "[数据包类型=" + PTypeCode.getName(packageType) +
				", 创建时间=" + new Date((long)date * 1000).toLocaleString() +
				", 设备标识符=" + deviceFlag +
				", 设备类型=" + DTypeCode.getName(deviceType) +
				", 数据长度=" + dataLength +
				", 数据=" + data + "]";
	}


	public Map<String,String> toMap()
	{
		Map<String,String> logMap= new HashMap<>();
		logMap.put("数据包类型",PTypeCode.getName(packageType));
		logMap.put("创建时间",new Date((long)date * 1000).toLocaleString());
		logMap.put("设备标识符",deviceFlag+"");
		logMap.put("设备类型",DTypeCode.getName(deviceType));
		logMap.put("数据长度",dataLength+"");
		logMap.put("数据","{"+data+"}");//数据用大括号裹起来
		return logMap;
	}
}
